using System;
using System.Text;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using iBeauty.BusinessObjects;

namespace iBeauty.DAL
{
	public class ServiceGroupDAO
	{
		public ServiceGroupDAO()
		{
			DbProviderHelper.GetConnection();
		}
		public List<ServiceGroup> GetServiceGroups()
		{
			try
			{
				List<ServiceGroup> lstServiceGroups = new List<ServiceGroup>();
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("SELECT [ID],[CreateDate],[ModifiedDate],[TimeStamp],[ServiceCode],[ServiceName],[AccountId],[Note] FROM [ServiceGroup]",CommandType.Text);
				DbDataReader oDbDataReader = DbProviderHelper.ExecuteReader(oDbCommand);
				while (oDbDataReader.Read())
				{
					ServiceGroup oServiceGroup = new ServiceGroup();
					oServiceGroup.ID = Convert.ToString(oDbDataReader["ID"]);

					if(oDbDataReader["CreateDate"] != DBNull.Value)
						oServiceGroup.CreateDate = Convert.ToDateTime(oDbDataReader["CreateDate"]);

					if(oDbDataReader["ModifiedDate"] != DBNull.Value)
						oServiceGroup.ModifiedDate = Convert.ToDateTime(oDbDataReader["ModifiedDate"]);

					if(oDbDataReader["TimeStamp"] != DBNull.Value)
						oServiceGroup.TimeStamp = Convert.ToDateTime(oDbDataReader["TimeStamp"]);

					if(oDbDataReader["ServiceCode"] != DBNull.Value)
						oServiceGroup.ServiceCode = Convert.ToString(oDbDataReader["ServiceCode"]);

					if(oDbDataReader["ServiceName"] != DBNull.Value)
						oServiceGroup.ServiceName = Convert.ToString(oDbDataReader["ServiceName"]);

					if(oDbDataReader["AccountId"] != DBNull.Value)
						oServiceGroup.AccountId = Convert.ToInt32(oDbDataReader["AccountId"]);

					if(oDbDataReader["Note"] != DBNull.Value)
						oServiceGroup.Note = Convert.ToString(oDbDataReader["Note"]);
					lstServiceGroups.Add(oServiceGroup);
				}
				oDbDataReader.Close();
				return lstServiceGroups;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public ServiceGroup GetServiceGroup(string ID)
		{
			try
			{
				ServiceGroup oServiceGroup = new ServiceGroup();
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("SELECT [ID],[CreateDate],[ModifiedDate],[TimeStamp],[ServiceCode],[ServiceName],[AccountId],[Note] FROM [ServiceGroup] WHERE [ID]=@ID",CommandType.Text);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ID",DbType.String,ID));
				DbDataReader oDbDataReader = DbProviderHelper.ExecuteReader(oDbCommand);
				while (oDbDataReader.Read())
				{
					oServiceGroup.ID = Convert.ToString(oDbDataReader["ID"]);

					if(oDbDataReader["CreateDate"] != DBNull.Value)
						oServiceGroup.CreateDate = Convert.ToDateTime(oDbDataReader["CreateDate"]);

					if(oDbDataReader["ModifiedDate"] != DBNull.Value)
						oServiceGroup.ModifiedDate = Convert.ToDateTime(oDbDataReader["ModifiedDate"]);

					if(oDbDataReader["TimeStamp"] != DBNull.Value)
						oServiceGroup.TimeStamp = Convert.ToDateTime(oDbDataReader["TimeStamp"]);

					if(oDbDataReader["ServiceCode"] != DBNull.Value)
						oServiceGroup.ServiceCode = Convert.ToString(oDbDataReader["ServiceCode"]);

					if(oDbDataReader["ServiceName"] != DBNull.Value)
						oServiceGroup.ServiceName = Convert.ToString(oDbDataReader["ServiceName"]);

					if(oDbDataReader["AccountId"] != DBNull.Value)
						oServiceGroup.AccountId = Convert.ToInt32(oDbDataReader["AccountId"]);

					if(oDbDataReader["Note"] != DBNull.Value)
						oServiceGroup.Note = Convert.ToString(oDbDataReader["Note"]);
				}
				oDbDataReader.Close();
				return oServiceGroup;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public int AddServiceGroup(string ID,DateTime?CreateDate,DateTime?ModifiedDate,DateTime?TimeStamp,string ServiceCode,string ServiceName,int?AccountId,string Note)
		{
			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("INSERT INTO [ServiceGroup]([CreateDate],[ModifiedDate],[TimeStamp],[ServiceCode],[ServiceName],[AccountId],[Note])VALUES(@CreateDate,@ModifiedDate,@TimeStamp,@ServiceCode,@ServiceName,@AccountId,@Note)",CommandType.Text);
				if (CreateDate.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@CreateDate",DbType.DateTime,CreateDate));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@CreateDate",DbType.DateTime,DBNull.Value));
				if (ModifiedDate.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ModifiedDate",DbType.DateTime,ModifiedDate));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ModifiedDate",DbType.DateTime,DBNull.Value));
				if (TimeStamp.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@TimeStamp",DbType.DateTime,TimeStamp));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@TimeStamp",DbType.DateTime,DBNull.Value));
				if (ServiceCode!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ServiceCode",DbType.String,ServiceCode));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ServiceCode",DbType.String,DBNull.Value));
				if (ServiceName!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ServiceName",DbType.String,ServiceName));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ServiceName",DbType.String,DBNull.Value));
				if (AccountId.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@AccountId",DbType.Int32,AccountId));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@AccountId",DbType.Int32,DBNull.Value));
				if (Note!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Note",DbType.String,Note));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Note",DbType.String,DBNull.Value));

				return DbProviderHelper.ExecuteNonQuery(oDbCommand);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public int AddServiceGroup(ServiceGroup oServiceGroup)
		{
			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("INSERT INTO [ServiceGroup]([CreateDate],[ModifiedDate],[TimeStamp],[ServiceCode],[ServiceName],[AccountId],[Note])VALUES(@CreateDate,@ModifiedDate,@TimeStamp,@ServiceCode,@ServiceName,@AccountId,@Note)",CommandType.Text);
				if (oServiceGroup.CreateDate.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@CreateDate",DbType.DateTime,oServiceGroup.CreateDate));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@CreateDate",DbType.DateTime,DBNull.Value));
				if (oServiceGroup.ModifiedDate.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ModifiedDate",DbType.DateTime,oServiceGroup.ModifiedDate));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ModifiedDate",DbType.DateTime,DBNull.Value));
				if (oServiceGroup.TimeStamp.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@TimeStamp",DbType.DateTime,oServiceGroup.TimeStamp));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@TimeStamp",DbType.DateTime,DBNull.Value));
				if (oServiceGroup.ServiceCode!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ServiceCode",DbType.String,oServiceGroup.ServiceCode));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ServiceCode",DbType.String,DBNull.Value));
				if (oServiceGroup.ServiceName!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ServiceName",DbType.String,oServiceGroup.ServiceName));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ServiceName",DbType.String,DBNull.Value));
				if (oServiceGroup.AccountId.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@AccountId",DbType.Int32,oServiceGroup.AccountId));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@AccountId",DbType.Int32,DBNull.Value));
				if (oServiceGroup.Note!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Note",DbType.String,oServiceGroup.Note));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Note",DbType.String,DBNull.Value));

				return DbProviderHelper.ExecuteNonQuery(oDbCommand);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public int UpdateServiceGroup(string ID,DateTime?CreateDate,DateTime?ModifiedDate,DateTime?TimeStamp,string ServiceCode,string ServiceName,int?AccountId,string Note)
		{

			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("UPDATE [ServiceGroup] SET [CreateDate]=@CreateDate,[ModifiedDate]=@ModifiedDate,[TimeStamp]=@TimeStamp,[ServiceCode]=@ServiceCode,[ServiceName]=@ServiceName,[AccountId]=@AccountId,[Note]=@Note WHERE [ID]=@ID",CommandType.Text);
				if (CreateDate.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@CreateDate",DbType.DateTime,CreateDate));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@CreateDate",DbType.DateTime,DBNull.Value));
				if (ModifiedDate.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ModifiedDate",DbType.DateTime,ModifiedDate));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ModifiedDate",DbType.DateTime,DBNull.Value));
				if (TimeStamp.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@TimeStamp",DbType.DateTime,TimeStamp));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@TimeStamp",DbType.DateTime,DBNull.Value));
				if (ServiceCode!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ServiceCode",DbType.String,ServiceCode));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ServiceCode",DbType.String,DBNull.Value));
				if (ServiceName!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ServiceName",DbType.String,ServiceName));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ServiceName",DbType.String,DBNull.Value));
				if (AccountId.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@AccountId",DbType.Int32,AccountId));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@AccountId",DbType.Int32,DBNull.Value));
				if (Note!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Note",DbType.String,Note));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Note",DbType.String,DBNull.Value));
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ID",DbType.String,ID));
				return DbProviderHelper.ExecuteNonQuery(oDbCommand);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public int UpdateServiceGroup(ServiceGroup oServiceGroup)
		{

			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("UPDATE [ServiceGroup] SET [CreateDate]=@CreateDate,[ModifiedDate]=@ModifiedDate,[TimeStamp]=@TimeStamp,[ServiceCode]=@ServiceCode,[ServiceName]=@ServiceName,[AccountId]=@AccountId,[Note]=@Note WHERE [ID]=@ID",CommandType.Text);
				if (oServiceGroup.CreateDate.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@CreateDate",DbType.DateTime,oServiceGroup.CreateDate));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@CreateDate",DbType.DateTime,DBNull.Value));
				if (oServiceGroup.ModifiedDate.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ModifiedDate",DbType.DateTime,oServiceGroup.ModifiedDate));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ModifiedDate",DbType.DateTime,DBNull.Value));
				if (oServiceGroup.TimeStamp.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@TimeStamp",DbType.DateTime,oServiceGroup.TimeStamp));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@TimeStamp",DbType.DateTime,DBNull.Value));
				if (oServiceGroup.ServiceCode!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ServiceCode",DbType.String,oServiceGroup.ServiceCode));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ServiceCode",DbType.String,DBNull.Value));
				if (oServiceGroup.ServiceName!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ServiceName",DbType.String,oServiceGroup.ServiceName));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ServiceName",DbType.String,DBNull.Value));
				if (oServiceGroup.AccountId.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@AccountId",DbType.Int32,oServiceGroup.AccountId));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@AccountId",DbType.Int32,DBNull.Value));
				if (oServiceGroup.Note!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Note",DbType.String,oServiceGroup.Note));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Note",DbType.String,DBNull.Value));
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ID",DbType.String,oServiceGroup.ID));
				return DbProviderHelper.ExecuteNonQuery(oDbCommand);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public int RemoveServiceGroup(ServiceGroup oServiceGroup)
		{

			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("DELETE FROM [ServiceGroup] WHERE [ID]=@ID",CommandType.Text);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ID",DbType.String,oServiceGroup.ID));
				return DbProviderHelper.ExecuteNonQuery(oDbCommand);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public int RemoveServiceGroup(string ID)
		{

			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("DELETE FROM [ServiceGroup] WHERE [ID]=@ID",CommandType.Text);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ID",DbType.String,ID));
				return DbProviderHelper.ExecuteNonQuery(oDbCommand);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
	}
}
